package com.nku.mason.lucene.util;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import com.nku.mason.lucene.entity.Article;

public class LuceneUtil {
	
	private static Directory directory;
	private static Version version;
	private static Analyzer analyzer;
	private static MaxFieldLength maxFieldLength;	
	
	public static Directory getDirectory() {
		return directory;
	}

	public static void setDirectory(Directory directory) {
		LuceneUtil.directory = directory;
	}

	public static Version getVersion() {
		return version;
	}

	public static void setVersion(Version version) {
		LuceneUtil.version = version;
	}

	public static Analyzer getAnalyzer() {
		return analyzer;
	}

	public static void setAnalyzer(Analyzer analyzer) {
		LuceneUtil.analyzer = analyzer;
	}

	public static MaxFieldLength getMaxFieldLength() {
		return maxFieldLength;
	}

	public static void setMaxFieldLength(MaxFieldLength maxFieldLength) {
		LuceneUtil.maxFieldLength = maxFieldLength;
	}

	//私有的构造方法防止外部直接new出工具类的对象
	private LuceneUtil(){
		//Do nothing ...
	}
	
	//静态初始化块
	static {
		try {
			directory = FSDirectory.open(new File("E:/TestDirectory/IndexDBa"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		version = Version.LUCENE_30;
		analyzer = new StandardAnalyzer(version);
		maxFieldLength = MaxFieldLength.LIMITED;	
	}
	
	/**
	 * javabean对象转document对象
	 * @param obj
	 * @return
	 */
	public static Document javabean2Document(Object obj){
		Document document = new Document();
		try {
			Class<? extends Object> clazz = obj.getClass();
			java.lang.reflect.Field[] fields = clazz.getDeclaredFields();
			for (int i = 0; i < fields.length; i++) {
				fields[i].setAccessible(true);
				String name = fields[i].getName();
				String methodName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
				Method method;

				method = clazz.getMethod(methodName, null);
				String value = method.invoke(obj, null).toString();
				Field docField = new Field(name, value, Store.YES, Index.ANALYZED);
				document.add(docField);
			}
			System.out.println("new Document created...");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return document;
	}
	
	/**
	 * document对象转Java对象
	 */
	public static Object document2JavaBean(Document document,Class<?> clazz){
		try {
			Object obj = clazz.newInstance();
			java.lang.reflect.Field[] declaredFields = clazz.getDeclaredFields();
			for (java.lang.reflect.Field declaredfield : declaredFields) {
				declaredfield.setAccessible(true);
				String name = declaredfield.getName();
				String value = document.get(name);
				//使用BeanUtils为javabean的属性赋值
				BeanUtils.setProperty(obj, name, value);
			}
			return obj;
		} catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	//测试
	public static void main(String[] args) {
		Article article = new Article(1, "南开", "南开大学排名每况愈下");
		Document document = LuceneUtil.javabean2Document(article);
		System.out.println(document.get("id"));
		System.out.println("------------------------------------");
		Object javaBean = LuceneUtil.document2JavaBean(document, Article.class);
		System.out.println(javaBean);
	}
}
